﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Configuration;


public partial class appform : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {

            ((AfterLogin)this.Master).mainCategory = "ACCOUNTS";

            //getting information for the login user

            if (Session["userId"] == null) return;

            this.tbFirstName.Text = Session["fName"] as String;
            this.tbLastName.Text = Session["lName"] as String;
            this.tbNric.Text = Session["custNRIC"] as String;
            this.tbNationality.Text = Session["custNationality"] as String;
            String gender = Session["custGender"] as String;
            if ("Female".Equals(gender))
            {
                this.rbFemale.Checked = true;
            }
            else
            {

            } this.rbMale.Checked = true;

            Object dob = Session["custDob"];
            if (dob != null)
            {

                this.tbDob.Text = ((DateTime)dob).ToString("dd/MM/yyyy");
            }

            this.tbPhone.Text = Session["custMobile"] as String;
            this.tbEmail.Text = Session["custEmail"] as String;
            this.tbAddress.Text = Session["custAddress"] as String;
            this.tbPostal.Text = Session["custPostal"] as String;
            this.tbOccupation.Text = Session["custOccupation"] as String;
            this.tbEffDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            String saving_acc = Session["savingAccountNo"] as String;
            this.tbAccountNo.Text = saving_acc;
            this.tbAccountNo.Enabled = false;

            if (saving_acc.StartsWith("520050"))
            {
                this.ddlSavingsAccountType.SelectedValue = "KidSaver";

            }
            else
            {

                this.ddlSavingsAccountType.SelectedValue = "PowerSave";


            }


            DateTime d1 = ((DateTime)dob);
            DateTime d2 = new DateTime(1995, 1, 1);
            if (d1 < d2)
            {
                ddlType.Items.Clear();
                ddlType.Items.Add("S$ Fixed Deposit Account");
            }
            else
            {
                ddlType.Items.Clear();
                ddlType.Items.Add("Fixed Monthly Deposit Account");
            }

            if (ddlType.SelectedItem.Text == "S$ Fixed Deposit Account")
            {
                ddlTenors.Items.Clear();
                ddlTenors.Items.Add("1 Month");
                ddlTenors.Items.Add("2 Months");
                ddlTenors.Items.Add("3 Months");
                ddlTenors.Items.Add("6 Months");
                ddlTenors.Items.Add("12 Months");
                ddlTenors.Items.Add("24 Months");
                ddlTenors.Items.Add("36 Months");
            }
            else
            {
                ddlTenors.Items.Clear();
                ddlTenors.Items.Add("12 Months");
                ddlTenors.Items.Add("24 Months");
                ddlTenors.Items.Add("36 Months");
            }
        }




    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        string fName = tbFirstName.Text;
        string lName = tbLastName.Text;
        string dof = tbDob.Text;
        string nationality = tbNationality.Text;
        string nric = tbNric.Text;
        string phone = tbPhone.Text;
        string email = tbEmail.Text;
        string add = tbAddress.Text;
        string postal = tbPostal.Text;
        string occupation = tbOccupation.Text;
        string accType = ddlType.SelectedItem.Text;
        decimal amount = decimal.Parse(tbAmount.Text);
        string tenor = ddlTenors.SelectedItem.Text;
        string effDate = tbEffDate.Text ;
        string savingAccType = ddlSavingsAccountType.SelectedItem.Text;
        string accNo = tbAccountNo.Text;
        //lbMessage.Text = "";
        //double dAmount = double.Parse(tbAmount.Text);
        //if (dAmount < 500)
        //{
        //    lbMessage.Text = "Minimium deposit amount have to be above $500";
        //}

        DateTime Month1 = DateTime.Now.AddMonths(1);
        DateTime Month2 = DateTime.Now.AddMonths(2);
        DateTime Month3 = DateTime.Now.AddMonths(3);
        DateTime Month6 = DateTime.Now.AddMonths(6);
        DateTime Month12 = DateTime.Now.AddMonths(12);
        DateTime Month24 = DateTime.Now.AddMonths(24);
        DateTime Month36 = DateTime.Now.AddMonths(36);

        //if (rbMale.Checked)
        //{ string gender = rbMale.Text; }
        //else
        //{ string gender = rbFemale.Text; }


        SqlConnection myConnection = DBManager.getSqlConnection();
        myConnection.Open();
        var gengerator = new AccountsManager();
        String fixed_acc;
        fixed_acc = gengerator.GenerateNo("FIXDEPOSIT");

        String fd_cert;
        fd_cert = gengerator.GenerateNo("FD_CERT");

        //SqlCommand myCommands = new SqlCommand("INSERT INTO ACCOUNTS(custID, accountNo, type) " +
        //"VALUES (@custID, @accountNo,  @type)", myConnection);
        //myCommands.Parameters.AddWithValue("@custID", Session["userID"] as String);
        //myCommands.Parameters.AddWithValue("@accountNo", fixed_acc);
        //myCommands.Parameters.AddWithValue("@type", "FIXDEPOSIT");
        //myCommands.ExecuteNonQuery();
       
        String strCommandText = "INSERT INTO FIXED_DEPOSIT(accountNo,fName, lName,dof, nationality, nric, phoneNo, Email, Address, PostalCode, Occupation," +
       " accountType, DepositAmount, tenors, effectiveDate, SavingsAccountType, SavingsAccountNo,custID,certNo,gender,maturityDate,status) " +
"VALUES (@accountNo,@fname, @lname, @DOB, @nationality, @nric, @phone, @email,@add,@postal,@occupation,@accType,@amount,@tenor,@effDate,@savingAccType,@savingAccNo,@custID,@certNo,@gender,@maturityDate,@status)";
        
        SqlCommand myCommand = new SqlCommand(strCommandText, myConnection);
       

        try
        {
            SqlCommand myCommand2 = new SqlCommand(strCommandText, myConnection);
            myCommand2.Parameters.AddWithValue("@accountNo", fixed_acc);
            myCommand2.Parameters.AddWithValue("@fname", tbFirstName.Text);
            myCommand2.Parameters.AddWithValue("@lName", tbLastName.Text);
            myCommand2.Parameters.AddWithValue("@DOB", Util.StringToDate(tbDob.Text));
            myCommand2.Parameters.AddWithValue("@nationality", tbNationality.Text);
            myCommand2.Parameters.AddWithValue("@nric", tbNric.Text);
            myCommand2.Parameters.AddWithValue("@phone", tbPhone.Text);
            myCommand2.Parameters.AddWithValue("@email", tbEmail.Text);
            myCommand2.Parameters.AddWithValue("@add", tbAddress.Text);
            myCommand2.Parameters.AddWithValue("@postal", tbPostal.Text);
            myCommand2.Parameters.AddWithValue("@occupation", tbOccupation.Text);
            myCommand2.Parameters.AddWithValue("@accType", ddlType.SelectedItem.Text);
            myCommand2.Parameters.AddWithValue("@amount", tbAmount.Text);
            myCommand2.Parameters.AddWithValue("@tenor", ddlTenors.SelectedItem.Text);
            myCommand2.Parameters.AddWithValue("@effDate", Util.StringToDate(tbEffDate.Text));
            myCommand2.Parameters.AddWithValue("@savingAccType", ddlSavingsAccountType.SelectedItem.Text);
            myCommand2.Parameters.AddWithValue("@savingAccNo", tbAccountNo.Text);
            myCommand2.Parameters.AddWithValue("@custID", Session["userID"] as String);
            myCommand2.Parameters.AddWithValue("@certNo", fd_cert);
            myCommand2.Parameters.AddWithValue("@status", "Active");
            if (rbFemale.Checked)
            { myCommand2.Parameters.AddWithValue("@gender", rbFemale.Text); }
            else
            { myCommand2.Parameters.AddWithValue("@gender", rbMale.Text); }


            if (ddlTenors.SelectedItem.Text == "1 Month")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month1);
            }
            if (ddlTenors.SelectedItem.Text == "2 Months")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month2);
            }
            if (ddlTenors.SelectedItem.Text == "3 Months")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month3);
            }
            if (ddlTenors.SelectedItem.Text == "6 Months")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month6);
            }
            if (ddlTenors.SelectedItem.Text == "12 Months")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month12);
            }
            if (ddlTenors.SelectedItem.Text == "24 Months")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month24);
            }
            if (ddlTenors.SelectedItem.Text == "36 Months")
            {
                myCommand2.Parameters.AddWithValue("maturityDate", Month36);
            }

            myCommand2.ExecuteNonQuery();


            SqlCommand myCommandm = new SqlCommand(strCommandText = "update [SAVING_ACCOUNT] SET [balance] = balance-" + amount + " WHERE [custID] =  '" + Session["userId"] + "'", myConnection);
            myCommandm.Parameters.AddWithValue("@balance", amount);
            myCommandm.ExecuteNonQuery();

            SqlCommand myCommandn = new SqlCommand("update [FIXED_DEPOSIT] SET [balance] = balance+" + amount + " WHERE [accountNo] = " + fixed_acc, myConnection);
            myCommandn.ExecuteNonQuery();

            string strType = "FIXED DEPOSIT PAYMENT";

            SqlCommand myCommandd = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);

            myCommandd.Parameters.AddWithValue("@date", DateTime.Now);
            myCommandd.Parameters.AddWithValue("@type", strType);
            myCommandd.Parameters.AddWithValue("@status", "SUCCESS");
            myCommandd.Parameters.AddWithValue("@accNo", tbAccountNo.Text);
            myCommandd.Parameters.AddWithValue("@transactionAmount", 0-amount);
            myCommandd.ExecuteNonQuery();



            SqlCommand myCommandi = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);

            myCommandi.Parameters.AddWithValue("@date", DateTime.Now);
            myCommandi.Parameters.AddWithValue("@type", strType);
            myCommandi.Parameters.AddWithValue("@status", "SUCCESS");
            myCommandi.Parameters.AddWithValue("@accNo", fixed_acc);
            myCommandi.Parameters.AddWithValue("@transactionAmount", amount);
            myCommandi.ExecuteNonQuery();




        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Response.Write(ex);
        }
        finally
        {
            myConnection.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('You have apply successfully!');window.location='certificate.aspx';", true);
        }

        }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("appform.aspx");
    }


}
       

    